package com.xsomnus.spring_framework.controller;

import com.xsomnus.spring_framework.edge.UserEdgeService;
import me.xsomnue.sample.starter.service.StarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author @叶小夏
 * @since 2019/8/8 0008 9:19
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    private static int counter = 100;



    @Autowired
    StarterService starterService;

    @Autowired
    UserEdgeService userEdgeService;

    @GetMapping("/notify")
    public String notify2() {
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " counter = " + counter);
        if (counter ++ % 7 == 0) {
            return "SUCCESS";
        }
        return "FAIL";
    }


    @GetMapping("/auto-starter")
    public void contextLoads() {
        String[] strings = starterService.split(",");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }

    @GetMapping("/counter")
    public String counter() {
        if (counter <= 0) {
            return "没有了";
        }
        counter--;
        System.out.println("counter: " + counter);
        return "恭喜抢到了" + counter;
    }


    @GetMapping("/add")
    public String add() {
        cpuCalc();
        return "oko";
    }


    @GetMapping("/test-pro")
    public String testProp(@RequestParam String name) {
        boolean jack = userEdgeService.decrease(name, 10);
        return "ok";
    }

    private void cpuCalc() {
        long sum = 0;
        for (long i = 0; i < 1000000000; i++) {
            sum = sum + i;
        }
        System.out.println("计算的结果是:" + sum);
    }




}
